/**
 * cn.ucox.web.application.config.RedisConfig
 *
 * @author chenw
 * @create 16/2/4.13:03
 * @email javacspring@hotmail.com
 */

package cn.com.oceansoft.application.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.nio.charset.Charset;

/**
 * Redis数据源配置
 *
 * @author chenw
 * @create 16/2/4 13:03
 * @email javacspring@gmail.com
 */
//@Configuration
//@Import(AppConfig.class)
//@PropertySource("classpath:properties/redis.properties")
public class RedisConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final static Marker MARKER_SPRING = MarkerFactory.getMarker("spring");

    @Value("${redis.host}")
    private String redisHost;

    @Value("${redis.port}")
    private int redisPort;

    @Value("${redis.pwd}")
    private String redisPassword;

    @Value("${redis.maxIdle}")
    private int redisMaxIdle;

    @Value("#{new Long(${redis.maxWaitMillis})}")
    private long redisMaxWaitMillis;

    @Value("#{new Integer(${redis.maxTotal})}")
    private int redisMaxTotal;

    @Value("#{new Boolean(${redis.testOnBorrow})}")
    private boolean redisTestOnBorrow;

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisMaxIdle);
        jedisPoolConfig.setMaxWaitMillis(redisMaxWaitMillis);
        jedisPoolConfig.setMaxTotal(redisMaxTotal);
        jedisPoolConfig.setTestOnBorrow(redisTestOnBorrow);
        return jedisPoolConfig;
    }

    @Bean(name = "jedisConnectionFactory")
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(redisHost);
        jedisConnectionFactory.setPort(redisPort);
        jedisConnectionFactory.setPassword(redisPassword);
        jedisConnectionFactory.setUsePool(true);
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
        return jedisConnectionFactory;
    }

    public StringRedisSerializer stringRedisSerializer() {
        return new StringRedisSerializer(Charset.forName("UTF-8"));
    }

    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(stringRedisSerializer());
        redisTemplate.setValueSerializer(stringRedisSerializer());
        redisTemplate.setHashKeySerializer(stringRedisSerializer());
        redisTemplate.setHashValueSerializer(stringRedisSerializer());
        return redisTemplate;
    }
}
